const jwt = require("jsonwebtoken");


const cheakToken = async (ctx) => {
    console.log(123456);
    let jwtToken = ctx.req.headers.authorization;
    let decoded;
    console.log(jwtToken);
    if(jwtToken) {
        let result = await verify(jwtToken);
        console.log(result, 123456);
        return result.authority;
        // console.log(decoded);
        // return decoded.authority;
    } else {
        ctx.body = {code: "500", msg: "请登录"}
    }
}

const verify = (token) => {
    return new Promise((rv, rj) => {
        jwt.verify(token, "shidaihaodang", function(err, decoded) {
            if(err) {
                ctx.body = {
                    code: 500,
                    msg: "token失效了"
                }
            } else {
                decoded = decoded;
                rv(decoded);
            }
        })
    })
}

exports.cheakToken=cheakToken;